package net.studou.service;

import java.util.Date;
import java.util.List;
import java.util.Set;

import javax.annotation.Resource;

import net.studou.dao.UserDao;
import net.studou.exception.SecretException;
import net.studou.exception.ShopKeeperException;
import net.studou.exception.UserExistException;
import net.studou.exception.UserForbidException;
import net.studou.exception.UserNotExistException;
import net.studou.po.Popedom;
import net.studou.po.User;
import net.studou.po.UserPopedom;
import net.studou.secret.SecretUtils;
import net.studou.tools.Utils;

import org.springframework.stereotype.Component;

@Component("userService")
public class UserService {

	@Resource(name = "userDao")
	private UserDao userDao;

	@Resource(name = "popedomService")
	private PopedomService popedomService;

	@Resource(name = "shopKeeperService")
	private ShopKeeperService shopKeeperService;

	public User regsiter(User pUser) throws UserExistException, UserNotExistException, ShopKeeperException {
		String userName = pUser.getUserName();

		isExist(userName);
		pUser.setParentUser(isNotExist(pUser.getParentUser().getUserName()));
		shopKeeperService.checkShopKeeper(pUser.getTbUrl(), pUser.getShopKeeper());

		Date now = Utils.getChinaDateTime();
		pUser.setStatus(0);
		pUser.setCreateTime(now);
		pUser.setMoney(0D);
		pUser.setPublishPoint(0D);
		pUser.setPublishPointReward(2D);
		pUser.setIntegral(0);
		pUser.setWarning(0);
		pUser.setOnoffline(1);
		pUser.setLoginTime(now);
		pUser.setModifyStatusTime(now);
		pUser.setLastLoginIP(null);
		pUser.setLastLoginTime(null);

		Set<UserPopedom> popedoms = pUser.getUserPopedoms();

		Popedom popedom = popedomService.getPopedomByCode("ad");
		if (popedom != null) {
			UserPopedom userPopedom = new UserPopedom();
			userPopedom.setUser(pUser);
			userPopedom.setPopedom(popedom);
			userPopedom.setEndTime(Utils.getDateByMonth(Utils.getChinaDateTime(), 120));
			userPopedom.setStatus(1);
			popedoms.add(userPopedom);
		}
		SecretUtils.encrypt(pUser);
		SecretUtils.signature(pUser);
		userDao.save(pUser);
		return pUser;
	}

	@SuppressWarnings("unchecked")
	public void isExist(String userName) throws UserExistException   {
		List<User> users = userDao.findByProperty("userName", userName);
		if (users != null && users.size() > 0) {
			throw new UserExistException("User[" + userName + "] have exist");
		}
	}

	@SuppressWarnings("unchecked")
	public User isNotExist(String userName) throws UserNotExistException  {
		if (userName == null || "".equals(userName)) {
			return null;
		}
		List<User> users = userDao.findByProperty("userName", userName);
		if (users == null || users.size() != 1) {
			throw new UserNotExistException("User[" + userName + "] not exist");
		} else {
			return users.get(0);
		}
	}

	@SuppressWarnings("unchecked")
	public User loginVerify(User pUser) throws UserNotExistException, UserForbidException {
		List<User> users = userDao.findByProperty("userName", pUser.getUserName());
		if (users == null || users.size() != 1) {
			throw new UserNotExistException("User[" + pUser.getUserName() + "] is not exist");
		} else {
			User dbUser = users.get(0);
			SecretUtils.decrypt(dbUser);
			if (!dbUser.getPassword().equals(pUser.getPassword())) {
				throw new UserNotExistException("User[" + pUser.getUserName() + "] password error");
			}
			
			SecretUtils.unSignature(dbUser);
			dbUser.setLastLoginIP(dbUser.getIp());
			dbUser.setLastLoginTime(dbUser.getLoginTime());

			dbUser.setLoginTime(Utils.getChinaDateTime());
			dbUser.setIp(pUser.getIp());
			
			SecretUtils.encrypt(dbUser);

			userDao.update(dbUser);
			
			int status = dbUser.getStatus();
			if (status == User.FORBID) {
				throw new UserForbidException("User[" + pUser.getUserName() + "] is forbidden", dbUser);
			} else if (status == User.LOCKED) {
				throw new SecretException("User is locked");
			}
			
			return dbUser;
		}
	}

	@SuppressWarnings("unchecked")
	public User getUserInfo(User pUser) throws UserForbidException, UserNotExistException {
		List<User> users = userDao.findByProperty("userName", pUser.getUserName());
		if (users == null || users.size() != 1) {
			throw new UserNotExistException("User[" + pUser.getUserName() + "] is not exist");
		} else {
			User dbUser = users.get(0);
			
			SecretUtils.unSignature(dbUser);
			dbUser.setLastLoginIP(pUser.getLastLoginIP());
			dbUser.setLastLoginTime(pUser.getLastLoginTime());
			
			int status = dbUser.getStatus();
			if (status == User.FORBID) {
				throw new UserForbidException("User[" + pUser.getUserName() + "] is forbidden", dbUser);
			} else if (status == User.LOCKED) {
				throw new SecretException("User is locked");
			}
			
			return dbUser;
		}
	}
	
	@SuppressWarnings("unchecked")
	public void lockUser(String userName) {
		List<User> users = userDao.findByProperty("userName", userName);
		if (users == null || users.size() == 1) {
			User pUser = users.get(0);
			// SecretUtils.decrypt(pUser);

			pUser.setStatus(User.LOCKED);
			//pUser.setModifyStatusTime(Utils.getChinaDateTime());

			// SecretUtils.encrypt(pUser);
			userDao.update(pUser);
		}
	}
}
